package com.lanp.springmybatis.config.database;

import com.alibaba.druid.pool.DruidDataSource;
import com.lanp.springmybatis.common.DataSourceCSV;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
/**
 * @author jiangpeng
 * Created with IntelliJ IDEA.
 * Description:  配置数据源
 * Date: 2022-03-04 16:00:08
 */
@Configuration
public class DataSourceConfig {
    /**
     * 主库，写
     */
    @Bean(DataSourceCSV.masterDataSource)
    @ConfigurationProperties(prefix = "dynamic.master.datasource")
    public DataSource masterDataSource() {
        System.out.println("create master datasource...");
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    /**
     * 从库只读
     */
    @Bean(DataSourceCSV.slaveDataSource)
    @ConfigurationProperties(prefix = "dynamic.slave3307.datasource")
    public DataSource slave3307DataSource() {
        System.out.println("create slave datasource...");
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    /**
     * 从库只读
     */
    @Bean(DataSourceCSV.slave3308DataSource)
    @ConfigurationProperties(prefix = "dynamic.slave3308.datasource")
    public DataSource slave3308DataSource() {
        System.out.println("create slave datasource...");
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
}
